#include "keysTest.h"

// ====================================================================================
// KeysTest 按键测试模块实现
// ====================================================================================

// 绘制按键测试UI框架
void drawKeys(Screen& screen, char *string) {
  // 清屏并绘制框架
  screen.spr.fillSprite(TFT_BLACK);
  screen.spr.drawString(string, 120, 20, 4);

  // 4个前端按键
  screen.spr.drawSmoothCircle(35, 75, 15, TFT_GREEN, TFT_BLACK);  // 抗锯齿的圆形
  screen.spr.drawSmoothCircle(80, 70, 10, TFT_GREEN, TFT_BLACK);
  screen.spr.drawSmoothCircle(150, 70, 10, TFT_GREEN, TFT_BLACK);
  screen.spr.drawSmoothCircle(205, 75, 15, TFT_GREEN, TFT_BLACK);

  // 电位器旋钮
  screen.spr.drawRect(35, 110, 75, 20, TFT_CYAN);
  screen.spr.drawRect(130, 110, 75, 20, TFT_CYAN);

  // 4个拨杆开关
  screen.spr.drawRect(20, 150, 20, 30, TFT_PINK);
  screen.spr.drawRect(60, 150, 20, 30, TFT_PINK);
  screen.spr.drawRect(160, 150, 20, 30, TFT_PINK);
  screen.spr.drawRect(200, 150, 20, 30, TFT_PINK);

  // 摇杆
  screen.spr.drawRect(10, 200, 100, 100, TFT_CYAN);
  screen.spr.drawRect(130, 200, 100, 100, TFT_CYAN);

  // 板载按键
  screen.spr.drawRect(80, 320, 20, 10, TFT_WHITE);
  screen.spr.drawRect(140, 320, 20, 10, TFT_WHITE);

  // 功能键
  screen.spr.drawRect(40, 360, 20, 20, TFT_SILVER);
  screen.spr.drawRect(40, 400, 20, 20, TFT_SILVER);
  screen.spr.drawRect(10, 380, 20, 20, TFT_SILVER);
  screen.spr.drawRect(70, 380, 20, 20, TFT_SILVER);
  screen.spr.drawRect(180, 360, 20, 20, TFT_SILVER);
  screen.spr.drawRect(180, 400, 20, 20, TFT_SILVER);
  screen.spr.drawRect(150, 380, 20, 20, TFT_SILVER);
  screen.spr.drawRect(210, 380, 20, 20, TFT_SILVER);

  // 陀螺仪
  screen.spr.drawRect(75, 440, 90, 90, TFT_VIOLET);

  // 把内容推到屏幕显示。 改方向后这里长款也要同步改
  screen.drawScreen();
}

// 更新按键测试界面的按键状态显示
void updateKeys(Screen& screen, Keys& keys) {
  keys.kvs_update();

  // 前端4个按键
  keys.kvs.L_up == 0 ? screen.spr.fillSmoothCircle(35, 75, 12, TFT_GREEN, TFT_BLACK) : screen.spr.fillSmoothCircle(35, 75, 12, TFT_BLACK, TFT_BLACK);
  keys.kvs.L_down == 0 ? screen.spr.fillSmoothCircle(80, 70, 7, TFT_GREEN, TFT_BLACK) : screen.spr.fillSmoothCircle(80, 70, 7, TFT_BLACK, TFT_BLACK);
  keys.kvs.R_down == 0 ? screen.spr.fillSmoothCircle(150, 70, 7, TFT_GREEN, TFT_BLACK) : screen.spr.fillSmoothCircle(150, 70, 7, TFT_BLACK, TFT_BLACK);
  keys.kvs.R_up == 0 ? screen.spr.fillSmoothCircle(205, 75, 12, TFT_GREEN, TFT_BLACK) : screen.spr.fillSmoothCircle(205, 75, 12, TFT_BLACK, TFT_BLACK);

  // 电位旋钮
  screen.spr.fillRect(36, 111, 73, 18, TFT_BLACK);
  screen.spr.fillRect(131, 111, 73, 18, TFT_BLACK);
  screen.spr.fillRect(map(keys.kvs.L_knob, -100, 100, 37, 108) - 2, 111, 4, 18, TFT_CYAN);
  screen.spr.fillRect(map(keys.kvs.R_knob, -100, 100, 132, 203) - 2, 111, 4, 18, TFT_CYAN);

  // 4个拨杆开关
  if (keys.kvs.switch_L1 == 1) {
    screen.spr.fillRect(21, 151, 18, 13, TFT_PINK);
    screen.spr.fillRect(21, 166, 18, 13, TFT_BLACK);
    // buzzer.on();
    // led.on();
  } else {
    screen.spr.fillRect(21, 151, 18, 13, TFT_BLACK);
    screen.spr.fillRect(21, 166, 18, 13, TFT_PINK);
    // buzzer.off();
    // led.off();
  }

  if (keys.kvs.switch_L2 == 1) {
    screen.spr.fillRect(61, 151, 18, 13, TFT_PINK);
    screen.spr.fillRect(61, 166, 18, 13, TFT_BLACK);
  } else {
    screen.spr.fillRect(61, 151, 18, 13, TFT_BLACK);
    screen.spr.fillRect(61, 166, 18, 13, TFT_PINK);
  }

  if (keys.kvs.switch_R1 == 1) {
    screen.spr.fillRect(161, 151, 18, 13, TFT_PINK);
    screen.spr.fillRect(161, 166, 18, 13, TFT_BLACK);
  } else {
    screen.spr.fillRect(161, 151, 18, 13, TFT_BLACK);
    screen.spr.fillRect(161, 166, 18, 13, TFT_PINK);
  }

  if (keys.kvs.switch_R2 == 1) {
    screen.spr.fillRect(201, 151, 18, 13, TFT_PINK);
    screen.spr.fillRect(201, 166, 18, 13, TFT_BLACK);
  } else {
    screen.spr.fillRect(201, 151, 18, 13, TFT_BLACK);
    screen.spr.fillRect(201, 166, 18, 13, TFT_PINK);
  }

  // 摇杆
  screen.spr.fillRect(11, 201, 98, 98, TFT_BLACK);
  screen.spr.fillRect(131, 201, 98, 98, TFT_BLACK);
  screen.spr.fillSmoothCircle(10 + map(keys.kvs.LX, -100, 100, 10, 90), 200 + map(keys.kvs.LY, -100, 100, 10, 90), 8, TFT_CYAN, TFT_BLACK);
  screen.spr.fillSmoothCircle(130 + map(keys.kvs.RX, -100, 100, 10, 90), 200 + map(keys.kvs.RY, -100, 100, 10, 90), 8, TFT_CYAN, TFT_BLACK);

  // 板载按键
  keys.kvs.board_L == 0 ? screen.spr.fillRect(81, 321, 18, 8, TFT_WHITE) : screen.spr.fillRect(81, 321, 18, 8, TFT_BLACK);
  keys.kvs.board_R == 0 ? screen.spr.fillRect(141, 321, 18, 8, TFT_WHITE) : screen.spr.fillRect(141, 321, 18, 8, TFT_BLACK);

  // 功能按键
  keys.kvs.up == 0 ? screen.spr.fillRect(41, 361, 18, 18, TFT_SILVER) : screen.spr.fillRect(41, 361, 18, 18, TFT_BLACK);
  keys.kvs.down == 0 ? screen.spr.fillRect(41, 401, 18, 18, TFT_SILVER) : screen.spr.fillRect(41, 401, 18, 18, TFT_BLACK);
  keys.kvs.left == 0 ? screen.spr.fillRect(11, 381, 18, 18, TFT_SILVER) : screen.spr.fillRect(11, 381, 18, 18, TFT_BLACK);
  keys.kvs.right == 0 ? screen.spr.fillRect(71, 381, 18, 18, TFT_SILVER) : screen.spr.fillRect(71, 381, 18, 18, TFT_BLACK);
  keys.kvs.x == 0 ? screen.spr.fillRect(181, 361, 18, 18, TFT_SILVER) : screen.spr.fillRect(181, 361, 18, 18, TFT_BLACK);
  keys.kvs.a == 0 ? screen.spr.fillRect(181, 401, 18, 18, TFT_SILVER) : screen.spr.fillRect(181, 401, 18, 18, TFT_BLACK);
  keys.kvs.o == 0 ? screen.spr.fillRect(151, 381, 18, 18, TFT_SILVER) : screen.spr.fillRect(151, 381, 18, 18, TFT_BLACK);
  keys.kvs.b == 0 ? screen.spr.fillRect(211, 381, 18, 18, TFT_SILVER) : screen.spr.fillRect(211, 381, 18, 18, TFT_BLACK);

  // 陀螺仪
  int angleX = map(keys.kvs.angleX, -100, 100, 446, 524);
  int angleY = map(keys.kvs.angleY, -100, 100, 81, 159);
  screen.spr.fillRect(76, 441, 88, 88, TFT_BLACK);
  screen.spr.fillRect(angleY - 5, angleX - 5, 10, 10, TFT_VIOLET);

  // 把内容推到屏幕显示。 改方向后这里长款也要同步改
  screen.drawScreen();
}
